<?php
$loader->requireOnce('includes/EwhValuatePrice/EwhValuateException.class.php');
$loader->requireOnce('includes/EwhValuatePrice/EwhValuatedEngine.class.php');
$loader->requireOnce('ordo/PhaEwhImticketDetail.class.php');

/**
 * Processor for valuate drug/material prices
 * @package modules.dept_pharmacy.includes.EwhValuatePrice
 * @author viethd
 * @version $Id$
 * @copyright 2009 LHV Company
 */
class EwhValuateProc {
    private $controller;
    private $logger ;
    
    function __construct($controller) {
        $this->controller = $controller;
        $this->logger = LoggerManager::getLogger('modules.dept_pharmacy.includes.EwhValuatePrice.EwhValuateProc');
        
    }
    
    public function allowSell($imTicketId, $imTicketDetailId, $allowSell) {
        $phaEwhImticketDetail = Celini::newOrdo('PhaEwhImticketDetail');         
         $phaEwhImticketDetail->allowSell($imTicketId, $imTicketDetailId, $allowSell);       
        
    }
    
   

    public function getArrItemsOfImTicket($imTicketId) {
        $phaEwhImticketDetail = Celini::newOrdo('PhaEwhImticketDetail');
        $result = $phaEwhImticketDetail->listDrugMaterialWithValPrice($imTicketId);
        $this->decorateItemsOfImTicket($result);

        return $result;
    }

    private function decorateItemsOfImTicket(&$data = array()){
        if (!is_array($data)) {
            return false;
        }
        $arrUnit = Utils::getEnumArrayKey(EnumerationConstant::PHA_IMSE_UNIT);        
        foreach ($data as &$item) {
            $item['im_expired_date_formatted'] = L10nHelper::renderDateTime($item['im_expired_date']);
            $item['im_unit_price_formatted'] = L10nHelper::renderNumber($item['im_unit_price']);
            $item['im_quantity_formatted'] = L10nHelper::renderNumber($item['im_quantity']);
            $item['remain_quantity_formatted'] = L10nHelper::renderNumber($item['remain_quantity']);
            $item['total_price_formatted'] = L10nHelper::renderNumber($item['total_price']);
            $item['insurance_price_formatted'] = L10nHelper::renderNumber($item['insurance_price']);
            $item['service_price_formatted'] = L10nHelper::renderNumber($item['service_price']);
            if (isset($arrUnit[$item['enum_unit_import_sell']])) {
                $item['enum_unit_import_sell_text'] = $arrUnit[$item['enum_unit_import_sell']];

            } else {
                $item['enum_unit_import_sell_text'] = '';
            }
        }

    }
    
    public function valuate($enumValuatePriceMethod, $metaData, $valuatingdata) {
        $valuatedMethod = EwhValuatedEngine::createValuatedMethod($enumValuatePriceMethod);
        $valuatedPricesResultArr = $valuatedMethod->valuatePrices($metaData, $valuatingdata);
        if (!is_array($valuatedPricesResultArr)) {
            return false;            
        }  
        
        foreach ($valuatedPricesResultArr as $item) {
        	if ($item instanceof ValuatedPricesResult) {
        	    $this->saveValuatedItem($enumValuatePriceMethod, $metaData, $item);
        	}
        }
    }
    
    private function saveValuatedItem($enumValuatePriceMethod, $metaData, $valuatedPricesResult) {
        //add new item into pha_ewh_valuate item
        //remove item in pha_ewh_valuate before save
        
        
        $newValuatedData = array(
        'pha_ewh_imticket_detail_id'    =>  $valuatedPricesResult->getImticketDetailId(),
        'valuator_user_id'              =>  $metaData['valuator_user_id'],        
        'date'                          =>  $metaData['date'],
        'enum_rate_type'                =>  $metaData['enum_rate_type'],
        'ref_imticket_detail_id'        =>  $valuatedPricesResult->getRefImticketDetailId(),
        'service_rate'                  =>  $metaData['service_rate'],
        'insurance_rate'                =>  $metaData['insurance_rate'],
        'enum_rate_type'                =>  $metaData['enum_rate_type'],
        'enum_valuate_method'           =>  $enumValuatePriceMethod
        );
        $phaEwhValuateObj = Celini::newOrdo('PhaEwhValuate');   
        $phaEwhValuateObj->deleteItem($valuatedPricesResult->getImticketDetailId());     
        $phaEwhValuateObj->setData($newValuatedData);
        $phaEwhValuateObj->persist();        
        //TODO check seccessful
        
        //update pha_ewh_imticket_detail
        $phaEwhImticketDetailObj = Celini::newOrdo('PhaEwhImticketDetail');
        $phaEwhImticketDetailObj->saveValuatedPrice(
            $valuatedPricesResult->getImticketDetailId(),
            $valuatedPricesResult->getInsurancePrice(),
            $valuatedPricesResult->getServicePrice(),
            $metaData['allow_sell'],
            PhaEwhImticketDetail::VALUATED            
        );
        
        //pha_ewh_imticket
        $phaEwhImticketObj = Celini::newOrdo('PhaEwhImticket');
        
        $phaEwhImticketObj->setValuated($metaData['imTicketId']);
    }
    

}
?>